$(function(){
	var SUCCESS_HTML = '<div id="surveysuccess" class="dialog-choose-exam-outer satisfactionSuccess" style="display: none;">' +
							'<div class="dialog-choose-exam">' +
								'<div  style="text-align: center; margin-bottom: 10px;"><img src="images/checksesterm_07.png"></div>' +
								'<div class="mb20" style="text-align: center; color: #E25C00;">感谢您的配合，我们会持续提升我们的服务！</div>' +
								'<div id="surveysuccessBtn" class="partPop_but footer-btn">确认</div>' +
							'</div>' + 
						'</div>';
	var OPINION_HTML = '<div id="surveyOpinion" class="dialog-choose-exam-outer satisfactionSuccess" style="display: none;">' +
							'<div class="dialog-choose-exam">' +
								'<div  style="text-align: center; margin-bottom: 10px;"><img name="icon"></div>' +
								'<div class="mb20" style="text-align: left; color: #E25C00;">您填写的反馈内容是我们提升服务的重要信息来源，您确认不填写您的反馈内容？</div>' +
								'<div>' +
								'<div id="surveyCancelBtn" style="float:left;" class="partPop_but footer-btn">返回填写</div>' +
								'<div id="surveySubmitBtn" style="float:right;" class="partPop_but footer-btn">确认不填</div>' +
								'</div>' +
						'</div>';
	var MASK = '<div id="satisfactionMask" class="mask" style="display: none;"></div>';
	var MASK_OPINION = '<div style="z-index: 10000;display: none;position: fixed; left: 0; top: 0; right: 0; bottom: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,.5); opacity: 0.5; filter: Alpha(opacity=50); background: #000000;"></div>';
	var DIALOG_HTML = '<div id="satisfaction" class="dialog-choose-exam-outer satisfaction" style="display: none;">' +
							'<div class="dialog-choose-exam">' +
								'<div class="dialog-head" style="background-color:#E25C00;"><div class="dialog-title" style="padding-top:12px;" ><span style="color:white;">学员满意度调查</span></div></div>' +
								'<div id="satisfactionTips" class="satisfactionTip"></div>' +
								'<div style="margin-bottom: 10px;"><span id="satisfactionLow"></span><span id="satisfactionHigh" style=float:right></span></div>' +
								'<div align="center" style="overflow: hidden;margin-bottom: 20px;">' +
									'<div class="card" id="scorebtn0">0</div>' +
									'<div class="card" id="scorebtn1">1</div>' +
									'<div class="card" id="scorebtn2">2</div>' +
									'<div class="card" id="scorebtn3">3</div>' +
									'<div class="card" id="scorebtn4">4</div>' +
									'<div class="card" id="scorebtn5">5</div>' +
									'<div class="card" id="scorebtn6">6</div>' +
									'<div class="card" id="scorebtn7">7</div>' +
									'<div class="card" id="scorebtn8">8</div>' +
									'<div class="card" id="scorebtn9">9</div>' +
									'<div class="card cardLast" id="scorebtn10">10</div>' +
								'</div>' +
								'<div id="opinion" style="display: none;">' +
									'<div id="opinionTextarea" style="margin-bottom:10px">' +
										'意见反馈' +
									'</div>' +
									'<div class="feedback" id="feedback">' +
										'<ul class="clearfix"></ul>' +
										'<div><input type="text" id="feedback-input" class="feedback-input" placeholder="请填写意见反馈"></div>' +
									'</div>' +
								'</div>' +
								'<div id="satisfactionBtn" class="partPop_but footer-btn submit-btn">提交</div>' +
							'</div>' +
						'</div>';


	var $dialog = $(DIALOG_HTML);
	var $mask = $(MASK);
	var $maskOpinion = $(MASK_OPINION);
	var $success = $(SUCCESS_HTML);
	var $opinionTip = $(OPINION_HTML);
	var feedbackListData;
	var feedbackList;
	var opinionTag = '';
	var timer = null;

	$('body').append($dialog);
	$('body').append($mask);
	$('body').append($maskOpinion);
	$('body').append($success);
	$('body').append($opinionTip);

	$dialog.on('click', 'div.card', function() {
		$dialog.find('div.card').removeClass('selected');
		$(this).addClass('selected');
		$dialog.find('#satisfactionTips').hide();
		$dialog.find('#opinion').show();
		$dialog.find('#satisfactionBtn').css('backgroundColor', '#E25C00')

		// 判断分数，显示对应意见标签
		if ($(this).html() <= feedbackListData[0].surveyScoreEnd) {
			feedbackList = feedbackListData[0].tagInfos;
		} else if ($(this).html() >= feedbackListData[2].surveyScoreBegin) {
			feedbackList = feedbackListData[2].tagInfos;
		} else {
			feedbackList = feedbackListData[1].tagInfos;
		}

 		var feedbackListHtml = '';
		for(var i = 0;i<feedbackList.length;i++) {
			feedbackListHtml += '<li data-tag-id="' + feedbackList[i].id + '">' + feedbackList[i].tagName + '</li>';
		}
		$dialog.find('#feedback ul').html(feedbackListHtml); 

	})

	$dialog.on('click', '.feedback li', function() {
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
		} else {
			$(this).addClass('selected');
		}
 	})

	$dialog.find('#feedback-input').focus(function() {
		clearInterval(timer);
		var $this = $(this);
		timer = setInterval(function() {
			if ($this.val().length >= 255) {
				$this.val($this.val().substring(0, 255));
			}
		}, 30);
	});
	$dialog.find('#feedback-input').blur(function() {
		clearInterval(timer);
	});

	$dialog.on('click', '#satisfactionBtn', function(){
		if($dialog.find('div.selected').length == 0){
			alert("请打分");
			return;
		}
		var score = $dialog.find('div.card.selected').text() - 0;
		var opinion = $.trim($dialog.find('#feedback-input').val());
		var opinionTagArr = [];

		// 拼tagId
		$dialog.find('.feedback li.selected').each(function(index, ele) {
			opinionTagArr.push($(ele).data('tag-id'))
		})
		opinionTag = opinionTagArr.join(',');
		
		//弹出哭脸或者笑脸的提示
		function opinionTipDialog(isCry){
			$opinionTip.find('img[name="icon"]').attr('src', 'images/' + (isCry ? 'cry.png': 'smile.png') + '');
			$opinionTip.show();
			$maskOpinion.show();
		}
		//“反馈内容”输入框填写内容为空时，需要给用户做一些提示
		if(opinion == '') {
			if(score >= 0 && score <= 6 ) {
				// 当学员选择0~6分时，点击提交按钮，则弹出哭脸弹窗。
				opinionTipDialog(true);
			} else if(score >=7 && score <= 10) {
				// 当学员选择7~10分时，点击提交按钮，则弹出笑脸弹窗。
				opinionTipDialog(false);
			}
		} else {
			process();
		}
	})

	//发送评分请求
	function process() {
		$.ajax({
			url: "new_pt_uc/my_lesson/processSatisfactionSurvey.action",
			type: "post",
			dataType: "json",
			data:{
				score: $dialog.find('div.card.selected').text(),
				opinion: $.trim($dialog.find('#feedback-input').val()),
				tagIds: opinionTag
			},
			cache: false,
			success: function(data){
				if(data.status=="success"){
					$dialog.remove();
					$success.show();
				}
			}
		});
	}

	$opinionTip.on('click', '#surveySubmitBtn', function(){
		process();
		$opinionTip.hide();
		$maskOpinion.hide();
	})

	$opinionTip.on('click', '#surveyCancelBtn', function(){
		$opinionTip.hide();
		$maskOpinion.hide();
	})

	$success.on('click', '#surveysuccessBtn', function(){
		$success.remove();
		$mask.remove();
		//弹出活动页
	})

	window.openSatisfactionDialog = function(){
		$.ajax({
			url: "new_pt_uc/my_lesson/satisfactionSurvey.action",
			type: "post",
			cache: false,
			success: function(data){
				if(data.isShow==1){
					//展示满意度调查弹窗
					$dialog.find('#satisfactionTips').text(data.content);
					$dialog.show();
					//mask
					$mask.show();
					switch(data.surveytypeid){
						case 1:
						case 2:
							$dialog.find('#satisfactionLow').html('很不满意');
							$dialog.find('#satisfactionHigh').html('非常满意');
						break;
						default:
							$dialog.find('#satisfactionLow').html('不可能');
							$dialog.find('#satisfactionHigh').html('很有可能');

					}
					feedbackListData = data.surveytypeTag;
				}
			}
		});
	}
})